﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace aklys {
	public partial class Aklys {
		public void Test(TextWriter stream) {
			var foo = new Dictionary<string, object>();
			var foo2 = new Dictionary<string, object>();
			foo["foo"] = foo2;
			foo2["bar"] = 3;
			foo2["baz"] = true;
			foo2["qux"] = "quux";
			conn.Transmit(foo);
			var echo = conn.Receive();
			if (echo == null) {
				stream.WriteLine("Nothing received.");
				return;
			}
			stream.WriteLine("Received:");
			PrettyPrint(stream, echo);
		}

		public void PrettyPrint(TextWriter stream, object o, int depth = 0) {
			var tab = "";
			var indent = depth + 2;
			while (depth-- > 0) tab += " ";
			if (o is Dictionary<string, object>) {
				var dict = (Dictionary<string, object>) o;
				stream.WriteLine("{0}(", tab);
				foreach (string k in dict.Keys) {
					stream.WriteLine("{0} [{1}]:", tab, k);
					PrettyPrint(stream, dict[k], indent);
				}
				stream.WriteLine("{0})", tab);
			} else if (o is string) {
				stream.WriteLine("{0}\"{1}\"", tab, o.ToString());
			} else {
				stream.WriteLine("{0}{1}", tab, o.ToString());
			}
		}

		public static void DebugMain(string[] args) {
			try {
				string hostname = null;
				int port = -1;
				if (args.Length > 0) {
					hostname = args[0];
				}
				if (args.Length > 1) {
					port = Convert.ToInt16(args[1]);
				}
				var aklys = new Aklys(hostname, port);
				//Console.WriteLine("cwd: {0}", Directory.GetCurrentDirectory());
				using (StreamWriter outfile = new StreamWriter(@"debug.txt")) {
					aklys.Test(outfile);
				}
			} catch (Exception e) {
				Console.WriteLine("Exception: {0}", e.Message);
				Console.ReadKey();
			}
		}
	}
}
